<form method="POST">
    <table>
        <tr>
            <td>Min Like </td> 
            <td><input type="text" name="min"></td>   
        </tr>
        <tr>
            <td>Max Like </td> 
            <td><input type="text" name="max"></td>
        </tr>
        <tr>
            <td>Keyword </td> 
            <td><input type="text" name="keyword"></td>
        </tr>
        <tr>
            <td>Company  </td> 
            <td><select name="id_company">
                    <option>-------------------------</option>
                    <?php
                    require_once '../model/model.php';
                    $query = "SELECT * FROM companies";
                    $companies = getPage($query);//$companies = '';
                    ?>
                    <?php foreach ($companies as $company): ?>  
                        <option value="<?php echo $company['id'] ?>">
                            <?php echo $company['name']; ?>     
                        <?php endforeach; ?>
                    </option>
                </select></td>
        </tr>
        <tr>
            <td>App :</td>
            <td><select name="id_app">
                    <option>----------------------------------------------------------------</option>
                    <?php
                    require_once '../model/model.php';
                    $query = "SELECT * FROM apps";
                    $apps = getPage($query);
                    ?>
                    <?php foreach ($apps as $app): ?>    
                        <option value="<?php echo $app['id'] ?>">
                            <?php echo $app['name']; ?>     
                        <?php endforeach; ?>
                    </option>
                </select></td>
        </tr>
    </table>
    <input type="submit" name="submit" value="Search">
</form>
<?php
require_once '../model/model.php';


$query = "SELECT * FROM pages LIMIT 20";
$pages = getPage($query);
?>
<?php if (isset($_POST['submit'])): ?>
    <?php $min = (double) $_POST['min']; ?>
    <?php $max = (double) $_POST['max']; ?>
    <?php $keyword = $_POST['keyword']; ?>
<?php endif; ?>
    <table border ="1">
        <tr>
            <th>Pid</th>
            <th>Image</th>
            <th>ID</th>
            <th>Name</th>
            <th>Username</th>
            <th>Phone</th>
            <th>Like</th>
        </tr>
        <?php foreach ($pages as $page): ?>
            <?php if (empty($keyword)) : ?>
                <?php if ((empty($max)) && (empty($min))) : ?>
                    <tr>
                        <td><?php echo $page['pid'] ?></td>
                        <td><img src='<?php echo $page['picture'] ?>'></td>
                        <td><?php echo $page['id'] ?></td>
                        <td><?php echo $page['name'] ?></td>
                        <td><?php echo $page['username'] ?></td>
                        <td><?php echo $page['about'] ?></td>
                        <td><?php echo $page['likes'] ?></td> 
                    </tr>
                <?php elseif (empty($max)) : ?>
                    <?php if ((int) $page['likes'] == $_POST['min']): ?>
                        <tr>
                            <td><?php echo $page['pid'] ?></td>
                            <td><img src='<?php echo $page['picture'] ?>'></td>
                            <td><?php echo $page['id'] ?></td>
                            <td><?php echo $page['name'] ?></td>
                            <td><?php echo $page['username'] ?></td>
                            <td><?php echo $page['about'] ?></td>
                            <td><?php echo $page['likes'] ?></td> 
                        </tr>
                    <?php endif; ?>
                <?php else : ?>
                    <?php if (((int) $page['like'] == $max) && ((int) $page['like'] == $min)) : ?>
                        <tr>
                            <td><?php echo $page['pid'] ?></td>
                            <td><img src='<?php echo $page['picture'] ?>'></td>
                            <td><?php echo $page['id'] ?></td>
                            <td><?php echo $page['name'] ?></td>
                            <td><?php echo $page['username'] ?></td>
                            <td><?php echo $page['about'] ?></td>
                            <td><?php echo $page['likes'] ?></td> 
                        </tr>
                    <?php endif; ?>
                <?php endif; ?>
            <?php else: ?>
                <?php if (strstr($page['name'], $keyword)) : ?>
                    <?php if (empty($max) && empty($min)) : ?>
                        <tr>
                            <td><?php echo $page['pid'] ?></td>
                            <td><img src='<?php echo $page['picture'] ?>'></td>
                            <td><?php echo $page['id'] ?></td>
                            <td><?php echo $page['name'] ?></td>
                            <td><?php echo $page['username'] ?></td>
                            <td><?php echo $page['about'] ?></td>
                            <td><?php echo $page['likes'] ?></td> 
                        </tr>
                    <?php elseif (empty($max)): ?>
                        <?php if ($max <= (int) $page['likes']) : ?>
                            <tr>
                                <td><?php echo $page['pid'] ?></td>
                                <td><img src='<?php echo $page['picture'] ?>'></td>
                                <td><?php echo $page['id'] ?></td>
                                <td><?php echo $page['name'] ?></td>
                                <td><?php echo $page['username'] ?></td>
                                <td><?php echo $page['about'] ?></td>
                                <td><?php echo $page['likes'] ?></td> 
                            </tr>
                        <?php endif; ?>
                    <?php else : ?>
                        <?php if (($max >= (int) $page['likes']) && ($min <= $page['likes'])): ?>
                            <tr>
                                <td><?php echo $page['pid'] ?></td>
                                <td><img src='<?php echo $page['picture'] ?>'></td>
                                <td><?php echo $page['id'] ?></td>
                                <td><?php echo $page['name'] ?></td>
                                <td><?php echo $page['username'] ?></td>
                                <td><?php echo $page['about'] ?></td>
                                <td><?php echo $page['likes'] ?></td> 
                            </tr>
                        <?php endif; ?>
                    <?php endif; ?>
                <?php endif; ?>
            <?php endif; ?>
        <?php endforeach; ?>
    </table>
